﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Grid.Extensions
{
    public class DataGridViewColorCell : DataGridViewTextBoxCell
    {
        public DataGridViewColorCell() : base()
        {
        }

        public override void InitializeEditingControl(int rowIndex, object 
            initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            // Set the value of the editing control to the current cell value.
            base.InitializeEditingControl(rowIndex, initialFormattedValue, 
                dataGridViewCellStyle);

            DataGridViewTextBoxEditingControl ctl =
                DataGridView.EditingControl as DataGridViewTextBoxEditingControl;
            ctl.ReadOnly = true;
            using (ColorDialog cd = new ColorDialog())
            {
                cd.Color = (Color)this.Value;
                cd.FullOpen = true;
                if (cd.ShowDialog() == DialogResult.OK)
                    this.Value = cd.Color;
                ctl.Text = cd.Color.Name;
                Style.BackColor = cd.Color;
                Style.ForeColor = Color.FromArgb(Color.White.ToArgb() - cd.Color.ToArgb());
            }
        }

        public override Type EditType
        {
            get
            {
                // Return the type of the editing contol that CalendarCell uses.
                return typeof(DataGridViewTextBoxEditingControl);
            }
        }

        public override Type ValueType
        {
            get
            {
                // Return the type of the value that CalendarCell contains.
                return typeof(Color);
            }
        }

        public override object DefaultNewRowValue
        {
            get
            {
                return Color.Black;
            }
        }

    }
}
